<template>
  <div>
    <BasicTable destroyOnClose={true} @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 消防隐患 </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'inspectionmanag:inHiddenDanger:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseId }}
        </a>
      </template>
      <template #abnormalPic="{ record }">
        <Image style="height: 100px;" v-if="record.abnormalPic" :src="record.abnormalPic" width="100" height="100" alt="" />
        <div style="height:100%;width:100%;" v-else>
          <img src="/@/assets/images/noData.png" style="width: 150px;height:auto;" alt="">
        </div>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button 
        style="margin-right: 5px;" 
        @click="handleForm({ tid: record.tid })" 
        type="text"
        >
          <span style="color: #4169E1;font-size:14px;">详情</span>
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace :maintainTaskId="MaintenanceTaskId" @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsInspectionmanagInHiddenDangerList">
  import { unref, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { inHiddenDangerDelete, inHiddenDangerListData } from '/@/api/inspectionmanag/inHiddenDanger';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { officeTreeData } from '/@/api/sys/office';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { Image } from 'ant-design-vue';


  const hiddenDangerName = ref('');
  const hiddenDangerParams = computed(() => {
    return {
      name: hiddenDangerName.value,
    };
  });
  const { t } = useI18n('inspectionmanag.inHiddenDanger');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const MaintenanceTaskId = ref('');

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('消防隐患表管理'),
  };

  const EnterpriseParams = ref<Recordable>({
    name: ''
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'reportTime',
        component: 'RangePicker',
        componentProps: {
          placeholder: [t('上报时间'), t('上报时间')],
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'enterpriseId',
        fieldLabel: 'enterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入所属主体'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'name',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入消防隐患名称'),
          allowClear: true,
          immediate: false,
        },
      },
      {
        label: t(''),
        field: 'status',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择当前隐患状态'),
          options: [
            { label: '已完成', value: '已完成' },
            { label: '未完成', value: '未完成' }
          ],
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t(''),
        field: 'reportPerson',
        component: 'Input',
        componentProps: {
          placeholder: t('上报人'),
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'alarmResult',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择报警结果'),
          options: [
            { label: '误报', value: '误报' },
            { label: '已处理', value: '已处理' }
          ],
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'overdue',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择超期情况'),
          options: [
            { label: '未超期', value: '未超期' },
            { label: '已超期', value: '已超期' }
          ],
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [['reportTime', ['beginDate', 'endDate']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所属主体'),
      dataIndex: 'enterpriseName',
      key: 'a.enterpriseName',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('地点'),
      dataIndex: 'address',
      key: 'a.address',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('异常图片'),
      dataIndex: 'abnormalPic',
      key: 'a.abnormal_pic',
      sorter: true,
      width: 130,
      align: 'left',
      slots: { customRender: 'abnormalPic' },
    },
    {
      title: t('隐患描述'),
      dataIndex: 'remark',
      key: 'a.remark',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('上报人'),
      dataIndex: 'reportPerson',
      key: 'a.report_person',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('上报时间'),
      dataIndex: 'reportTime',
      key: 'a.report_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('当前状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      // dictType:'sys_search_status',
    },
    {
      title: t('报警结果'),
      dataIndex: 'alarmResult',
      key: 'a.alarm_result',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('超期情况'),
      dataIndex: 'overdue',
      key: 'a.overdue',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 160,
      fixed: 'right',
      slots: { customRender: 'action' }, // 使用自定义插槽
    },
  ];

  const [registerTable, { reload }] = useTable({
    api: inHiddenDangerListData,
    beforeFetch: (params) => {
      const urlParams = new URLSearchParams(router.currentRoute.value.fullPath.split('?')[1]);
      const tid = urlParams.get('tid');
      if (tid) {
        params.tid = decodeURIComponent(tid);
      }
      if (params.reportTime) {
        const [beginDate, endDate] = params.reportTime;
        params.beginDate = beginDate;
        params.endDate = endDate;
        delete params.reportTime; // 删除原始的 reportTime 参数
      }
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    // console.log('record:', record)
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await inHiddenDangerDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    if (record.maintenanceTaskId) {
      console.log('MaintenanceTaskId:', record.maintenanceTaskId);
      MaintenanceTaskId.value = record.maintenanceTaskId;
    }
    traceModel(true, { formKey: 'Fire_Hazard_Process', bizKey: record.taskId });
  }
</script>